package com.baoteng.base.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoteng.base.R;

/**
 * Author: loren
 * Date: 2019/9/27
 */
public class TopTitleBar extends FrameLayout {

    private TextView title;
    private ImageView right;
    private TextView rightTv;
    private ImageView left;

    public TopTitleBar(@NonNull Context context) {
        this(context, null);
    }

    public TopTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public TopTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        View view = inflate(getContext(), R.layout.view_top_title_bar, this);
        title = view.findViewById(R.id.top_title_tv);
        right = view.findViewById(R.id.top_right_iv);
        rightTv = view.findViewById(R.id.top_right_Tv);
        left = view.findViewById(R.id.top_left_iv);
    }

    public void setTitle(String text) {
        if (!TextUtils.isEmpty(text) && title != null) {
            title.setText(text);
        }
    }

    public void setRightClickListener(OnClickListener listener) {
        if (listener != null && right != null) {
            right.setVisibility(VISIBLE);
            right.setOnClickListener(listener);
        }
    }

    public void setLeftClickListener(OnClickListener listener) {
        if (listener != null && left != null) {
            left.setVisibility(VISIBLE);
            left.setOnClickListener(listener);
        }
    }

    public void setLeftVisibility(int visibility) {
        if (left != null) {
            left.setVisibility(visibility);
        }
    }

    public void setRightResource(int resId) {
        if (right != null) {
            right.setImageResource(resId);
        }
    }

    public void setRightTextClickListener(OnClickListener listener) {
        if (rightTv != null && listener != null) {
            rightTv.setVisibility(VISIBLE);
            rightTv.setOnClickListener(listener);
        }
    }

    public void setRightText(String text) {
        if (rightTv != null) {
            rightTv.setText(text);
        }
    }

    public void setRightTextVisible(boolean visible) {
        if (rightTv != null) {
            rightTv.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }
}
